﻿using BLL.Sys;
using Common;
using Enums;
using Enums.Sys;
using Model.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DAGG.Controllers.Sys
{
    public class LoginController : Controller
    {
        //
        // GET: /Login/
        public ActionResult Index()
        {
            Session.Abandon();//清除全部Session
            return View();
        }
        /// <summary>
        /// 新增编辑方法
        /// </summary>
        /// <param name="userModel"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CheckLogin(SysUserModel userModel)
        {
            ResultEntity result = new ResultEntity();
            SysUserBLL sysUserBLL = new SysUserBLL();
            SysUserModel sysUserModel = sysUserBLL.GetModel(userModel.LoginName);
            if (sysUserModel == null || sysUserModel.ID <= 0)
            {
                result.ResultState = Enums.EnumResultState.Failed;
                result.Msg = "系统中不存在此帐户！";
                return Json(result);
            }
            else
            {
                //是否是否停用
                if (sysUserModel.ViewFlag == EnumManagement.GetEnumValue(SysEnum.Disable))
                {
                    result.ResultState = Enums.EnumResultState.Failed;
                    result.Msg = "用户已停用！";
                    return Json(result);
                }
                //是否过有效期
                if (PageValidate.ConvertToDateTime(sysUserModel.DisableTime.Value.ToString("yyyy-MM-dd")) < PageValidate.ConvertToDateTime(DateTime.Now.ToString("yyyy-MM-dd")))
                {
                    result.ResultState = Enums.EnumResultState.Failed;
                    result.Msg = "用户已过有效期！";
                    return Json(result);
                }
                
                if (EDS3.DESEnCode(userModel.Pwd) != sysUserModel.Pwd)
                {
                    result.ResultState = Enums.EnumResultState.Failed;
                    result.Msg = "密码错误！";
                    return Json(result);
                }

                Session.Add("SysUser", sysUserModel);
                Session.Timeout = 30;
                return Redirect("/Index/MainNew");
                
            }
        }

	}
}